﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

using zijian666.WorkScheduler;
using zijian666.WorkScheduler.Abstractions;

using var host = Host.CreateDefaultBuilder(args)
    //.ConfigureHostConfiguration(config =>
    //{
    //    config.AddEnvironmentVariables("DOTNET_");
    //    config.AddJsonFile("appsettings.json", optional: true);
    //    config.AddCommandLine(args);
    //})
    .ConfigureServices((context, services) =>
    {
        services.AddLogging(builder =>
        {
            builder.AddSystemdConsole();
            builder.SetMinimumLevel(LogLevel.Trace);
        });
        services.AddWorkScheduler<MyWorkManager>("default", c =>
        {
            c.BatchSize = 100;
            c.Expires = TimeSpan.FromSeconds(30);
            c.Threads = 10;
            c.RefreshInterval = TimeSpan.FromSeconds(10);
            c.SetWorkHandle<MyWorkHandler>();
        });
    })
    .Build();

_ = host.RunAsync();

using var workservice = host.Services.GetWorkService("default");
_ = workservice.Start(default);

_ = Task.Factory.StartNew(async () =>
{
    var manager = workservice.GetRequiredService<IWorkManager>();
    while (true)
    {
        await Task.Delay(1000);
        Console.Title = manager.ToString()!;
    }
});


while (true)
{
    Console.WriteLine("新增数据(条): ");
    if (int.TryParse(Console.ReadLine(), out var count))
    {
        var manager = (MyWorkManager)workservice.GetRequiredService<IWorkManager>();
        try
        {
            manager.AppendData(count);
            workservice.Refresh();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
}
